//-----------------------------------------------------------------------
//<copyright file="SanPhamDAO.cs
//Code generate by tools.
//</copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using PMS.Exceptions;
using System.Data.SqlClient;
using PMS.Entity;

namespace PMS.DAL
{
	public class SanPhamDAO : SQLDatabase, ISanPhamDAO
	{

		#region Create SanPham methods

		public SanPham CreateSanPham(SanPham sanPham)
		{
			try
			{

				SqlCommand command = SQLDatabase.StoredProcedureCommand("spSanPhamInsert");
				AddInParameter(command, "@MaThuoc", SqlDbType.NVarChar, sanPham.MaThuoc);
				AddInParameter(command, "@TenHoatChat", SqlDbType.NVarChar, sanPham.TenHoatChat);
				AddInParameter(command, "@TenBietDuoc", SqlDbType.NVarChar, sanPham.TenBietDuoc);
				AddInParameter(command, "@NongDoHamLuong", SqlDbType.NVarChar, sanPham.NongDoHamLuong);
				AddInParameter(command, "@DangBaoChe", SqlDbType.NVarChar, sanPham.DangBaoChe);
				AddInParameter(command, "@QuyCach", SqlDbType.NVarChar, sanPham.QuyCach);
				AddInParameter(command, "@SoDangKy", SqlDbType.NVarChar, sanPham.SoDangKy);
				AddInParameter(command, "@XuatXu", SqlDbType.NVarChar, sanPham.XuatXu);
				AddInParameter(command, "@NhaSX", SqlDbType.NVarChar, sanPham.NhaSX);
				AddInParameter(command, "@NhaNK", SqlDbType.NVarChar, sanPham.NhaNK);
				AddInParameter(command, "@GhiChu", SqlDbType.NVarChar, sanPham.GhiChu);
				AddInParameter(command, "@MaDonVi", SqlDbType.NVarChar, sanPham.MaDonVi);
				AddInParameter(command, "@MaNH", SqlDbType.NVarChar, sanPham.MaNH);

				ExecuteNonQuery(command);
				return sanPham;
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access CreateSanPhamException", ex);
			}
		}

		#endregion

		#region Update SanPham methods

		public void UpdateSanPham(SanPham sanPham)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spSanPhamUpdate");

				AddInParameter(command, "@MaThuoc", SqlDbType.NVarChar, sanPham.MaThuoc);
				AddInParameter(command, "@TenHoatChat", SqlDbType.NVarChar, sanPham.TenHoatChat);
				AddInParameter(command, "@TenBietDuoc", SqlDbType.NVarChar, sanPham.TenBietDuoc);
				AddInParameter(command, "@NongDoHamLuong", SqlDbType.NVarChar, sanPham.NongDoHamLuong);
				AddInParameter(command, "@DangBaoChe", SqlDbType.NVarChar, sanPham.DangBaoChe);
				AddInParameter(command, "@QuyCach", SqlDbType.NVarChar, sanPham.QuyCach);
				AddInParameter(command, "@SoDangKy", SqlDbType.NVarChar, sanPham.SoDangKy);
				AddInParameter(command, "@XuatXu", SqlDbType.NVarChar, sanPham.XuatXu);
				AddInParameter(command, "@NhaSX", SqlDbType.NVarChar, sanPham.NhaSX);
				AddInParameter(command, "@NhaNK", SqlDbType.NVarChar, sanPham.NhaNK);
				AddInParameter(command, "@GhiChu", SqlDbType.NVarChar, sanPham.GhiChu);
				AddInParameter(command, "@MaDonVi", SqlDbType.NVarChar, sanPham.MaDonVi);
				AddInParameter(command, "@MaNH", SqlDbType.NVarChar, sanPham.MaNH);

				ExecuteNonQuery(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access UpdateSanPhamException", ex);
			}
		}

		#endregion

		#region Delete SanPham methods

		public void DeleteSanPham(SanPham sanPham)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spSanPhamDelete");

				AddInParameter(command, "@MaThuoc", SqlDbType.NVarChar, sanPham.MaThuoc);

				ExecuteNonQuery(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access DeleteSanPhamException", ex);
			}
		}

		#endregion

		#region Select methods

		#region GetAll SanPham methods

		public SanPhams GetAllSanPham()
		{
			try
			{

				SqlCommand command = SQLDatabase.StoredProcedureCommand("spSanPhamGetList");
				SanPhams Result = new SanPhams();
				List<SanPham> List = ExecuteQuery<SanPham>(command,true);
				foreach(SanPham item in List)
				{
					Result.Add(item);
				}
				return Result;
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access GetAllSanPhamException", ex);
			}
		}

        public SanPhams GetAllSanPhamCoNhapKho()
        {
            try
            {

                SqlCommand command = SQLDatabase.StoredProcedureCommand("GetAllSanPhamCoNhapKho");
                SanPhams Result = new SanPhams();
                List<SanPham> List = ExecuteQuery<SanPham>(command, true);
                foreach (SanPham item in List)
                {
                    Result.Add(item);
                }
                return Result;
            }
            catch (Exception ex)
            {
                throw DataAccessException.GetException("Data Access GetAllSanPhamException", ex);
            }
        }

		#endregion


		#region GetSanPham methods

		public SanPham GetSanPhamByID(string maThuoc)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spSanPhamGet");

				AddInParameter(command, "@maThuoc", SqlDbType.NVarChar, maThuoc);

				return ExecuteQuery<SanPham>(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access GetSanPhamBy ID Exception", ex);
			}
		}

		#endregion


		#endregion
	}
}//End Class
